package com.cjkj.system.controller;

import com.cjkj.common.core.domain.R;
import com.cjkj.common.core.web.controller.BaseController;
import com.cjkj.common.core.web.domain.AjaxResult;
import com.cjkj.common.core.web.page.TableDataInfo;
import com.cjkj.common.log.annotation.Log;
import com.cjkj.common.log.enums.BusinessType;
import com.cjkj.common.security.annotation.InnerAuth;
import com.cjkj.common.security.annotation.RepeatSubmit;
import com.cjkj.common.security.annotation.RequiresPermissions;
import com.cjkj.common.security.utils.SecurityUtils;
import com.cjkj.system.api.domain.APIHospitalEquipment;
import com.cjkj.system.api.domain.dto.APIEquipmentStatusDto;
import com.cjkj.system.domain.dto.HospitalEquipmentDistributionReq;
import com.cjkj.system.domain.dto.HospitalEquipmentReq;
import com.cjkj.system.service.ITHospitalEquipmentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 设备管理控制器
 */
@RestController
@RequestMapping("/equipment")
public class HospitalEquipmentController extends BaseController {

    @Resource
    private ITHospitalEquipmentService hospitalEquipmentService;

    /**
     * 查询设备列表数据
     * @param req
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(HospitalEquipmentReq req){
        startPage();
        req.setHospitalId(SecurityUtils.getLoginUser().getSysUser().getHospitalId());
        if(req.getDeptId() == null) {
            req.setDeptId(SecurityUtils.getLoginUser().getSysUser().getDeptId());
        }
        return getDataTable(hospitalEquipmentService.selectTHospitalEquipmentList(req));
    }

    /**
     * 分配设备
     * @param req
     * @return
     */
    @PostMapping("/distribution")
    @RepeatSubmit
    @RequiresPermissions("hospital:equipment:distribution")
    @Log(title = "医院设备管理",businessType = BusinessType.UPDATE)
    public AjaxResult distributionEquipment(@RequestBody HospitalEquipmentDistributionReq req){
        req.setHospitalId(SecurityUtils.getLoginUser().getSysUser().getHospitalId());
        return AjaxResult.success(hospitalEquipmentService.distributionEquipment(req));
    }

    @GetMapping("expireData")
    public AjaxResult expireData(String equipmentType){
        return AjaxResult.success(hospitalEquipmentService.getExpireData(equipmentType));
    }

    @GetMapping("getFaultyEquipment")
    public AjaxResult getFaultyEquipment(String equipmentType){
        return AjaxResult.success(hospitalEquipmentService.getFaultyEquipment(equipmentType));
    }

    /**
     * 获取下拉选项数据
     * @param equipmentType 设备类型
     * @return
     */
    @GetMapping("getOptionData")
    public AjaxResult getOptionData(String equipmentType){
        return AjaxResult.success(hospitalEquipmentService.getEquipmentOptionData(equipmentType));
    }

    @InnerAuth
    @GetMapping("get/{equipmentId}")
    public R<APIHospitalEquipment> get(@PathVariable("equipmentId") String equipmentId){
        return R.ok(hospitalEquipmentService.getHospitalEquipment(equipmentId));
    }

    @InnerAuth
    @PostMapping("/update/status")
    public R<Integer> updateEquipmentStatus(@RequestBody APIEquipmentStatusDto dto){
        return R.ok(hospitalEquipmentService.updateEquipmentStatus(dto));
    }

}
